package com.opensky.qatest.ui.page;

import java.util.List;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.selenium.Report;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "Wishlist page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class WishlistPage extends LoadableComponent<WishlistPage> implements IPage {

    private static final String WISHLIST_PAGE_URL = "https://opensky.com/account/wishlist";

    private static final String WISHLIST_ITEM_IMG = "//td[@class='item-thumbnail']//img";

    private static final String REMOVE_ITEM_BTN = "//table[@class='wish-list']//input[@value='Remove']";

    private static final String REMOVE_MESSAGE = "//div[@id='notification' and contains(., 'has been removed from your list')]";

    public static final String LOGO = "//div[@id='logo']//img";

    public static final long TIMEOUT = 3;

    private final WebDriverWrapper driver;

    public WishlistPage(WebDriverWrapper driver) {
	this.driver = driver;
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(WISHLIST_PAGE_URL);
    }

    public void removeAllWishlistItems() {
	List<WebElement> removeBtns = driver.waitAssertAndGetElements("REMOVE_ITEM_BTN", By.xpath(REMOVE_ITEM_BTN));

	for (WebElement btn : removeBtns) {
	    btn.click();
	    driver.pause(TIMEOUT);
	}
    }

    public void removeWishlistItem() {
	driver.waitAndClick("REMOVE_ITEM_BTN", By.xpath(REMOVE_ITEM_BTN));
    }

    public SalesPage clickLogo() {
	SalesPage salesPage = new SalesPage(driver);

	driver.waitAndClick("LOGO", By.xpath(LOGO));

	if (salesPage.isPageLoaded()) {
	    return salesPage;
	} else {
	    return null;
	}
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() throws Error {
	String url = driver.getCurrentUrl();
	Assert.assertTrue(Report.wrongURL(WISHLIST_PAGE_URL, url), LogicUtils.isURLEqual(WISHLIST_PAGE_URL, url));
    }

    public boolean isPageLoaded() {
	String url = driver.getCurrentUrl();
	return LogicUtils.isURLEqual(WISHLIST_PAGE_URL, url);
    }

    public boolean isWishlistItemPresent() {
	driver.waitAndVerifyElementPresent("WISHLIST_ITEM_IMG", By.xpath(WISHLIST_ITEM_IMG));
	return true;
    }

    public boolean isRemoveMessgePresent() {
	driver.waitAndVerifyElementPresent("REMOVE_MESSAGE", By.xpath(REMOVE_MESSAGE));
	return true;
    }

    public boolean isCategoryDropDownItemBaseElementsPresent() {

	return true;
    }

    // ************************ GET/SET methods **************************

    @Override
    public String getName() {
	return "Wishlist page";
    }
}
